#!/usr/bin/env bash

# Check consistency of /etc/slurm/topology.conf with nodelist in /etc/slurm/slurm.conf
# See "man topology.conf".
# Homepage: https://github.com/OleHolmNielsen/Slurm_tools/

SLURMCONF=/etc/slurm/slurm.conf
TOPOLOGYCONF=/etc/slurm/topology.conf
if test ! -s $SLURMCONF
then
	echo Slurm file $SLURMCONF not found
	exit 1
fi
if test ! -s $TOPOLOGYCONF
then
	echo Slurm file $TOPOLOGYCONF not found
	exit 1
fi

export TOPOLOGY_LIST=/tmp/topology.conf_list.$$
export NODELIST=/tmp/slurm.conf_list.$$

echo Extract nodelist from $TOPOLOGYCONF
export TOPOLOGY_ALLNODES=`grep -i Nodes= $TOPOLOGYCONF | grep -v '^\s*#' | awk -F= '{printf("%s,", $3)}'`
scontrol show hostnames $TOPOLOGY_ALLNODES | sort -V > $TOPOLOGY_LIST

# Get current Slurm node list
# (Nodes in multiple partitions will be printed multiple times)
sinfo -h -N -o %N | sort -V | uniq > $NODELIST

echo Differences between nodelists in $SLURMCONF and $TOPOLOGYCONF:
diff -is -c $NODELIST $TOPOLOGY_LIST

# Cleanup
rm -f $TOPOLOGY_LIST $NODELIST
